﻿using System;
using ZYLFramework.Manage;

namespace Tool
{
    using System.Collections.Generic;
    using UnityEditor;
    using UnityEngine;
    
    public class Tool : MonoBehaviour
    {
        private const string PrefabPathHome = "Prefabs/";
        
        public static bool NewUIGameObject(string path, GameObject panel, out GameObject item)
        {
            if (panel == null)
            {
                item = null;
                return false;
            }

            item = null;
            var temp = ResourcesManage.Instance.LoadAsset(PrefabPathHome+path) as GameObject;

            if (temp == null)
            {
                return false;
            }
            
            item = Instantiate(temp);
            item.transform.SetParent(panel.transform, false);
            return true;
        }
        
        public static void SetVisible(GameObject item,bool isVisible)
        {
            item.transform.localScale = isVisible ? new Vector3(1,1,1): new Vector3(0,0,0);
        }
        
        /// <summary>
        /// 世界坐标转UI坐标
        /// </summary>
        /// <param name="mainCamera">世界的摄像机</param>
        /// <param name="uiCamera">UI的摄像机</param>
        /// <param name="worldPos">世界坐标</param>
        /// <param name="uiRect">当前的canvas</param>
        /// <returns></returns>
        public static Vector3 WorldPos2UIPos(Camera mainCamera,Camera uiCamera,Vector3 worldPos,RectTransform uiRect)
        {
            //世界转屏幕 mainCamera世界的摄像机
            Vector3 screenPosition  = mainCamera.WorldToScreenPoint(worldPos);
            //屏幕转UI  ui(当前的canvas)  uiCamera(UI的摄像机)
            if (RectTransformUtility.ScreenPointToLocalPointInRectangle(uiRect, screenPosition , uiCamera, out var worldPoint))
            {
                Debug.Log("UI的本地坐标为：" + worldPoint);
                return worldPoint;
            }
            return worldPoint;
        }
        
        public static void TimeLog(string log)
        {
            DateTime NowTime = DateTime.Now.ToLocalTime();
            var timeStr = NowTime.ToString("yyyy-MM-dd HH:mm:ss");
            Debug.Log($"[{timeStr}]:{log}");
        }
        
        /// <summary>
        /// 随机获取枚举值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T RandomEnum<T>()
        {
            T[] results = Enum.GetValues(typeof(T)) as T[];
            System.Random random = new System.Random(GetRandomSeed());
            T result = results[random.Next(1, results.Length)];
            return result;
        }
    
        /// <summary>
        /// 创建加密随机数生成器 生成强随机种子
        /// </summary>
        /// <returns></returns>
        private static int GetRandomSeed()
        {
            byte[] bytes = new byte[4];
            System.Security.Cryptography.RNGCryptoServiceProvider rng
                = new System.Security.Cryptography.RNGCryptoServiceProvider();
            rng.GetBytes(bytes);
            return BitConverter.ToInt32(bytes, 0);
        }
        
        public static void CopyValue(object origin,object target)
        {
            System.Reflection.PropertyInfo[] properties = (target.GetType()).GetProperties();
            System.Reflection.FieldInfo[] fields = (origin.GetType()).GetFields();
            for ( int i=0; i< fields.Length; i++)
            {
                for ( int j=0; j< properties.Length; j++)
                {
                    if (fields[i].Name == properties[j].Name && properties[j].CanWrite)
                    {
                        properties[j].SetValue(target,fields[i].GetValue(origin),null);
                    }
                }
            }
        }
        
        public static void RemoveArray(object origin,object target)
        {
            int[] arr = new int[] { 1, 2, 3, 4, 5 };
            // 复制数组
            int[] newArr = new int[arr.Length - 1];
            Array.Copy(arr, 1, newArr, 0, arr.Length - 1);
            // 重新调整数组大小
            Array.Resize(ref arr, newArr.Length);
            // 将新数组的元素复制到旧数组中
            Array.Copy(newArr, arr, newArr.Length);
        }
    }
}
